/*=============================================================================

  APPLICATION MANAGER MODULE

  FILE: UImgr.cpp
  
  DESCRIPTION

    Manager Module Definition

  EXTERNALIZED FUNCTIONS

    
  Copyright (c) DaeSung Engineering Inc. All rights reserved.

=============================================================================*/
/*=============================================================================

                             EDIT HISTORY

  ---------  -------  -------------------------------------------------------
    when       who     what, where, why
  ---------  -------  -------------------------------------------------------
   06/28/03   nso4th   Version 0.1

=============================================================================*/

/*=============================================================================
  HEADER INCLUSION
=============================================================================*/
#include <UImgr_MOD.h>

/*=============================================================================
  CONSTANT IMPLEMENTATION
=============================================================================*/
#define MON_SYNCWAIT_TIME			(200) // 1 sec

/*=============================================================================
  LOCALIZED FUNCTION DEFINITION
=============================================================================*/

/*=============================================================================
  UI MONITORING DEFINITION
=============================================================================*/
void UImgr::MON_Alloc(void)
{
	m_monSync = Sync_Alloc();
	m_monLock = TRUE;
	m_reqLock = FALSE;
	m_reqWait = FALSE;

	m_monCnt = 0;
	m_curSel = m_preSel = FALSE;
}

void UImgr::MON_Free(void)
{
	if(m_monSync)
	{
		Sync_Free(m_monSync);
		m_monSync = NULL;
	}
}

void UImgr::MON_STATE(void)
{
	if(UIIsInit() == FALSE)
		return;
	
	if((m_monCnt % 2) == 0)
	{
		MON_DEVICE();
	}
	MON_MOTION();

	m_monCnt++;
}

void UImgr::MON_LOCK(boolean lock)
{
	m_reqWait = TRUE;
	m_reqLock = lock;

	Sync_Set(m_monSync, FALSE);
	Task_PutSignal(m_pTask, TASK_SIG_SYNC);
	if(Sync_Wait(m_monSync, MON_SYNCWAIT_TIME) == FALSE)
	{
		//DBGPRINT("[UImgr] MON_LOCK.....time-out\n");
		m_reqWait = FALSE;
		m_reqLock = FALSE;
		m_monLock = TRUE;
	}
}

void UImgr::MON_DEVICE(void)
{
	CMainView		*vw = (CMainView*)ObjList_Get(VIEW_MAIN);
	SYSmgr			*sm = (SYSmgr*)ObjList_Get(SYS_MGR);
	HIO				*io = (HIO*)ObjList_Get(HIO_DEV);
	HAIO			*aio = (HAIO*)ObjList_Get(HAIO_DEV);
	HMot			*mot = (HMot*)ObjList_Get(HMOT_DEV);
	HFgDev			*fg = (HFgDev*)ObjList_Get(HFG_DEV);
	HCAM			*cam = (HCAM*)ObjList_Get(HCAM_DEV);
	CFormSts		*fs;
	dword			mjSts;
	int32			j;

	if((vw == NULL) || (sm == NULL))
		return;

	fs = &vw->m_frmSts;

	mjSts = sm->State_MajCurrent();
	if(mjSts == DEV_S)
	{
		for(j=FST_LTXT_MOT; j<=FST_LTXT_CAM; j++)
		{
			UILtxt_Style(&fs->m_gLTxt[j], UI_LED_GRAY_ST);	
		}
	}

	// Motion
	if((mot != NULL) && (mot->IsDeviceAvail()))
	{
		UILtxt_Style(&fs->m_gLTxt[FST_LTXT_MOT], UI_LED_GREEN_ST);
	}
	else
	{
		UILtxt_Style(&fs->m_gLTxt[FST_LTXT_MOT], UI_LED_REDBLINK_ST);
	}

	// Camera
	if((fg != NULL) && (fg->IsDeviceAvail()))
	{
		UILtxt_Style(&fs->m_gLTxt[FST_LTXT_FG], UI_LED_GREEN_ST);
	}
	else
	{
		UILtxt_Style(&fs->m_gLTxt[FST_LTXT_FG], UI_LED_REDBLINK_ST);
	}

	// Camera Interface
	if((cam != NULL) && (cam->IsDeviceAvail()))
	{
		UILtxt_Style(&fs->m_gLTxt[FST_LTXT_CAM], UI_LED_GREEN_ST);
	}
	else
	{
		UILtxt_Style(&fs->m_gLTxt[FST_LTXT_CAM], UI_LED_REDBLINK_ST);
	}

	// I/O
	if((io != NULL) && (io->IsDeviceAvail()))
	{
		UILtxt_Style(&fs->m_gLTxt[FST_LTXT_DIO], UI_LED_GREEN_ST);
	}
	else
	{
		UILtxt_Style(&fs->m_gLTxt[FST_LTXT_DIO], UI_LED_REDBLINK_ST);
	}

	// ANALOG I/O
	if((aio != NULL) && (aio->IsDeviceAvail()))
	{
		UILtxt_Style(&fs->m_gLTxt[FST_LTXT_AIO], UI_LED_GREEN_ST);
	}
	else
	{
		UILtxt_Style(&fs->m_gLTxt[FST_LTXT_AIO], UI_LED_REDBLINK_ST);
	}
}

void UImgr::MON_MOTION(void)
{
	HMot			*mot = (HMot*)ObjList_Get(HMOT_DEV);
	CMainView		*vw = (CMainView*)ObjList_Get(VIEW_MAIN);
	CFormSts		*fs;
	Vsetup			*vs = (Vsetup*)ObjList_Get(VIEW_VSETUP);
	HMOTvw			*mv = (HMOTvw*)ObjList_Get(VIEW_MOTDEV);
	int32			j, axNum;
	boolean			done, alarm, on;
	double64			pos, vel;
	int32			rate;
	
	if((mot==NULL) || (vw == NULL) || (vs == NULL))
		return;

	fs = &vw->m_frmSts;
	if(::IsWindow(fs->m_hWnd) == FALSE)
		return;
	
	if(mot->IsHomeDone() == TRUE)
	{
		UILtxt_Style(&fs->m_gLTxt[FST_LTXT_HOME], UI_LED_GREEN_ST);
	}
	else
	{
		UILtxt_Style(&fs->m_gLTxt[FST_LTXT_HOME], UI_LED_REDBLINK_ST);
	}

	axNum = mot->GetAxisNum() ;		// MOT_AXIS_2 : for slave axis

	//-------------------------------------------
	// POSITION & VELOCITY & LOAD RATE
	//-------------------------------------------
	{
		static char		str_buf[128];
		char				*str;

		str = &str_buf[0];

		for(j=0; j<axNum; j++)
		{
			done = mot->GetPosition(j, &pos);
			if(done == FALSE)
			{
				sprintf(str, "---.---");
				pos = 0.0;
			}
			else
			{
				sprintf(str, "%.3f", pos);
			}

			// STS
			fs->m_gTxt[FST_TXT_XP+j].SetText(str);

			// HMOTvw
			mv->SIG_set_position(j, pos);

			// HMOTvw..Velocity
			if(mot->GetVelocityActual(j, &vel) == TRUE)
			{
				mv->SIG_set_speed(j, vel);
			}
			else
			{
				mv->SIG_set_speed(j, 0.0);
			}
			done = mot->LoadRateGet(j, &rate);
			if(done == FALSE)
			{
				sprintf(str, "---");
				rate = 0;
			}
			else
			{
				sprintf(str, "%d", rate);
			}

			// STS
			//fs->m_gTxt[FST_TXT_XB+j].SetText(str);
		}
	}

	//-------------------------------------------
	// MOVE-DONE & ALARM
	//-------------------------------------------
	{
		for(j=0; j<axNum; j++)		
		{
			alarm = mot->IsAlarm(j);
			done = mot->IsMoveDone(j);
			//done = mot->IsVelZero(j);

			// STS
			if(alarm)
			{
				UILtxt_Style(&fs->m_gLTxt[FST_LTXT_XU+j], UI_LED_REDBLINK_ST);
			}
			else
			{
				if(done)
				{
					UILtxt_Style(&fs->m_gLTxt[FST_LTXT_XU+j], UI_LED_GRAY_ST);
				}
				else
				{
					UILtxt_Style(&fs->m_gLTxt[FST_LTXT_XU+j], UI_LED_BLUE_ST);
				}
			}

			// HMotVw
			mv->SIG_set(j, HAXVW_SIG_INPOS, done);
			if(alarm)
			{
				mv->SIG_set(j, HAXVW_SIG_ALARM, HAXVW_SIG_ERR);
			}
			else
			{
				mv->SIG_set(j, HAXVW_SIG_ALARM, FALSE);
			}
		}
	}
	//-------------------------------------------------
	// // LIMIT, HOME SIGNAL, SERVO & HOMING
	//-------------------------------------------------
	{
		for(j=0; j<axNum; j++)
		{
			// N-LIMIT
			if(mot->SigGet(j, MOT_SIG_NLIMIT, &on) == TRUE)
			{
				mv->SIG_set(j, HAXVW_SIG_NLIMIT, on);
			}
			else
			{
				mv->SIG_set(j, HAXVW_SIG_NLIMIT, HAXVW_SIG_ERR);
			}
			// P-LIMIT
			if(mot->SigGet(j, MOT_SIG_PLIMIT, &on) == TRUE)
			{
				mv->SIG_set(j, HAXVW_SIG_PLIMIT, on);
			}
			else
			{
				mv->SIG_set(j, HAXVW_SIG_PLIMIT, HAXVW_SIG_ERR);
			}
			// HOME
			if(mot->SigGet(j, MOT_SIG_ORIGIN, &on) == TRUE)
			{
				mv->SIG_set(j, HAXVW_SIG_HOME, on);
			}
			else
			{
				mv->SIG_set(j, HAXVW_SIG_HOME, HAXVW_SIG_ERR);
			}

			// SERVO
			if(mot->IsServoOn(j) == TRUE)
			{
				mv->SIG_set(j, HAXVW_SIG_SERVO, TRUE);
			}
			else
			{
				mv->SIG_set(j, HAXVW_SIG_SERVO, FALSE);
			}
			// Homming
			/*
			if(mot->IsHomming(j))
			{
			mv->SIG_set(j, HAXVW_SIG_HOMMING, TRUE);
			}
			else
			{
			mv->SIG_set(j, HAXVW_SIG_HOMMING, FALSE);
			}
			*/
		}
	}
	
}

/*--- END OF UImgr_MON.CPP ---*/
